package com.mybatis.mapper;

import com.mybatis.entity.Country;
import com.mybatis.entity.SysRole;
import com.mybatis.entity.SysRoleExtend;
import com.mybatis.entity.SysUser;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.w3c.dom.ls.LSException;

import javax.jws.soap.SOAPBinding;
import java.util.*;

public class UserMapperTest extends BaseMapperTest{
    private static SqlSessionFactory sqlSessionFactory;



    @Test
    public void testSelectByid(){
        SqlSession sqlSession = getSqlSession();
        try{
            // 获取usermapper 接口
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser sysUser = userMapper.selectById(1L);
            assert "admin".equals(sysUser.getUserName());
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectAll(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<SysUser> userList = userMapper.selectAll();
            assert ! userList.isEmpty();
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectRolesByUserId(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<SysRole> roleList = userMapper.selectRolesByUserId(1001L);
            roleList.forEach(System.out::println);
        } finally {
            sqlSession.close();
        }
    }

    @Test
    // 这个没有成功，不知道为啥返回的还是sysrole 对象
    public void testSelectRolesWithUserNameByUserId(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<SysRoleExtend> sysRoleExtends = userMapper.selectRolesWithUserNameByUserId(1001L);
            sysRoleExtends.forEach(System.out::println);
        } finally {
            sqlSession.close();
        }
    }


    @Test
    public void testInsert(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser sysUser = new SysUser();
            sysUser.setUserName("test1").setUserPassword("123456").setUserEmail("test@mybatis.com").setUserInfo("test info").setHeadImg(new byte[]{1,2,3}).setCreateTime(new Date());
            int insert = userMapper.insert(sysUser);
            assert insert == 1;
            // assert sysUser.getId() != null;
        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }
    }

    @Test
    public void testInsert2(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser sysUser = new SysUser();
            sysUser.setUserName("test1").setUserPassword("123456").setUserEmail("test@mybatis.com").setUserInfo("test info").setHeadImg(new byte[]{1,2,3}).setCreateTime(new Date());
            int insert = userMapper.insert2(sysUser);
            assert insert == 1;
            assert sysUser.getId() != null;
        } finally {
            // sqlSession.rollback();
            sqlSession.commit();
            sqlSession.close();
        }
    }

    @Test
    public void testInsert3(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser sysUser = new SysUser();
            sysUser.setUserName("test1").setUserPassword("123456").setUserEmail("test@mybatis.com").setUserInfo("test info").setHeadImg(new byte[]{1,2,3}).setCreateTime(new Date());
            int insert = userMapper.insert3(sysUser);
            assert insert == 1;
            assert sysUser.getId() != null;
        } finally {
            // sqlSession.rollback();
            sqlSession.commit();
            sqlSession.close();
        }
    }

    @Test
    public  void   testUpdateById(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser user = userMapper.selectById(1L);
            assert "admin".equals(user.getUserName());
            user.setUserName("admin_test").setUserEmail("test@mybatis.com");
            int result = userMapper.updateById(user);
        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }
    }

    @Test
    public  void   testDeleteById(){
        SqlSession sqlSession =getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser user1 = userMapper.selectById(1L);
            assert 1 ==  userMapper.deleteById(1L);
            assert userMapper.selectById(1L) == null;
        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }

    }



    @Test
    public void testSelectRolesByUserIdAndRoleEnabled(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<SysRole> userList = userMapper.selectRolesByUserIdAndRoleEnabled(1L, 1);
            assert userList.size() > 0;
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectRolesByUserAndRole(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser sysUser = new SysUser();
            SysRole sysRole = new SysRole();
            sysUser.setId(1L);
            sysRole.setEnabled(1L);
            List<SysRole> userList = userMapper.selectRolesByUserAndRole(sysUser,sysRole);
            assert userList.size() > 0;
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectByUser(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            // 只查询用户名
            SysUser query = new SysUser();
            query.setUserName("ad");
            List<SysUser> userList = userMapper.selectByUser(query);
            assert !userList.isEmpty();
            // 只查询邮箱
            query  = new SysUser();
            query.setUserEmail("test@mybatis.tk");
            userList = userMapper.selectByUser(query);
            assert !userList.isEmpty();
            // 同时查询这两个
            query = new SysUser();
            query.setUserName("ad").setUserEmail("test@mybatis.tk");
            userList = userMapper.selectByUser(query);
            assert userList.isEmpty();
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public  void testInsert4Selective(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser user = new SysUser();
            user.setUserName("test-selective").setUserPassword("1234556").setUserInfo("test info").setCreateTime(new Date());
            userMapper.insert4(user);
            user = userMapper.selectById(user.getId());
        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }
    }

    @Test
    public void testelectByIdOrUserName(){
        SqlSession sqlSession = getSqlSession();
        try  {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            SysUser query = new SysUser();
            query.setId(1L).setUserName("admin");
            SysUser user = userMapper.selectByIdOrUserName(query);
            assert user != null;
            query.setId(null);
            user = userMapper.selectByIdOrUserName(query);
            assert user!= null;
            query.setUserName(null);
            user = userMapper.selectByIdOrUserName(query);
            assert user == null;
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectByUser4(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            // 只查询用户名
            SysUser query = new SysUser();
            query.setUserName("ad");
            List<SysUser> userList = userMapper.selectByUser(query);
            assert !userList.isEmpty();
            // 只查询邮箱
            query  = new SysUser();
            query.setUserEmail("test@mybatis.tk");
            userList = userMapper.selectByUser(query);
            assert !userList.isEmpty();
            // 同时查询这两个
            query = new SysUser();
            query.setUserName("ad").setUserEmail("test@mybatis.tk");
            userList = userMapper.selectByUser(query);
            assert userList.isEmpty();
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectByIdList(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<Long> idList = new ArrayList<Long>();
            idList.add(1L);
            idList.add(1001L);
            List<SysUser> userList = userMapper.selectByIdList(idList);
            assert userList.size() == 2;
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void testInsertList(){
        SqlSession sqlSession = getSqlSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<SysUser> userList = new ArrayList<SysUser>();
            for (int j = 0; j < 2 ; j++){
                SysUser user = new SysUser();
                user.setUserName("test" + j);
                user.setUserPassword("123455");
                user.setUserEmail("test@mybatis.tk");
                userList.add(user);
            }
            int result = userMapper.insertList(userList);
            assert result == 2;
        } finally {
            // sqlSession.commit();
            sqlSession.rollback();
            sqlSession.close();
        }
    }


    @Test
    public void testUpdateByMap(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", 1L);
            map.put("user_email", "test@mybatis.tk");
            userMapper.updateByMap(map);
            SysUser user = userMapper.selectById(1L);
        } finally {
            sqlSession.rollback();
            sqlSession.commit();
        }
    }

    @Test
    public void testSelectByUserBind(){
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            // 只查询用户名
            SysUser query = new SysUser();
            query.setUserName("ad");
            List<SysUser> userList = userMapper.selectByUserBind(query);
            assert !userList.isEmpty();
            // 只查询邮箱
            query  = new SysUser();
            query.setUserEmail("test@mybatis.tk");
            userList = userMapper.selectByUserBind(query);
            assert !userList.isEmpty();
            // 同时查询这两个
            query = new SysUser();
            query.setUserName("ad").setUserEmail("test@mybatis.tk");
            userList = userMapper.selectByUserBind(query);
            assert userList.isEmpty();
        } finally {
            sqlSession.close();
        }
    }

}
